package com.ghovos.usermangerpgdemo.contorller.api;

import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import com.ghovos.usermangerpgdemo.entity.PermissionEntity;
import com.ghovos.usermangerpgdemo.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liuli
 */
@RestController
@RequestMapping("/permission")
@AllArgsConstructor
@Api(tags = "权限管理api")
public class PermissionController {

    private final PermissionService permissionService;
    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 查出所有permission 包括额外信息
     * @return permission集合
     */
    @ApiOperation(("查出所有permission 包括额外权限"))
    @GetMapping("/all/ext")
    public GlobalResult<List<PermissionEntity>> allExt(){
        return GlobalResult.ok(permissionService.getAllPermissionExt());
    }

    /**
     * 通过id查询  包括额外权限信息
     * @param id permissionId
     * @return permission
     */
    @ApiOperation("通过id查询  包括额外信息")
    @GetMapping("/find/ext/{id}")
    public GlobalResult<PermissionEntity> getExtById(@PathVariable("id") Long id){
        return GlobalResult.ok(permissionService.getPermissionExtById(id));
    }

    /**
     * 添加或更新权限
     * @param permissionEntity permissionEntity
     * @return 添加成功后的实体
     */
    @ApiOperation("添加或更新权限")
    @PostMapping(value = {"/update", "/add"})
    public GlobalResult<Object> addOrSave(@RequestBody PermissionEntity permissionEntity){
        permissionService.saveOrUpdateWithRedis(permissionEntity);
        return GlobalResult.ok(permissionEntity);
    }


    /**
     * 根据 id 删除 权限
     * @param id 权限id
     * @return 删除是否成功
     */
    @ApiOperation("根据id删除权限")
    @GetMapping("/delete/{id}")
    public GlobalResult<Object> deleteById(@PathVariable("id") Long id){
        return GlobalResult.ok(permissionService.deleteByIdWithRole(id));
    }
}
